package com.lcp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lcp.common.Result;
import com.lcp.entity.TcmKnowledge;
import com.lcp.service.TcmKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tcm-knowledge")
public class TcmKnowledgeController {

    @Autowired
    private TcmKnowledgeService tcmKnowledgeService;

    @GetMapping("/{id}")
    public Result<TcmKnowledge> getById(@PathVariable Long id) {
        tcmKnowledgeService.incrementViewCount(id);
        return Result.success(tcmKnowledgeService.getById(id));
    }

    @GetMapping("/page")
    public Result<IPage<TcmKnowledge>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String keyword) {
        return Result.success(tcmKnowledgeService.page(pageNum, pageSize, category, keyword));
    }

    @PostMapping
    public Result<TcmKnowledge> create(@RequestBody TcmKnowledge knowledge) {
        return Result.success(tcmKnowledgeService.create(knowledge));
    }

    @PutMapping("/{id}")
    public Result<TcmKnowledge> update(@PathVariable Long id, @RequestBody TcmKnowledge knowledge) {
        knowledge.setId(id);
        return Result.success(tcmKnowledgeService.update(knowledge));
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        tcmKnowledgeService.delete(id);
        return Result.success();
    }

    @PostMapping("/{id}/like")
    public Result<Void> like(@PathVariable Long id) {
        tcmKnowledgeService.incrementLikeCount(id);
        return Result.success();
    }

    @GetMapping("/category-statistics")
    public Result<List<Map<String, Object>>> getCategoryStatistics() {
        return Result.success(tcmKnowledgeService.getCategoryStatistics());
    }

    @GetMapping("/hot")
    public Result<List<TcmKnowledge>> getHotKnowledge(
            @RequestParam(defaultValue = "10") Integer limit) {
        return Result.success(tcmKnowledgeService.getHotKnowledge(limit));
    }

    @GetMapping("/{id}/related")
    public Result<List<TcmKnowledge>> getRelatedKnowledge(
            @PathVariable Long id,
            @RequestParam(defaultValue = "5") Integer limit) {
        return Result.success(tcmKnowledgeService.getRelatedKnowledge(id, limit));
    }
} 